/**
 * 时间日期工具类
 */
import moment from 'moment'
window.moment = moment
moment.locale('zh-cn')

const formatSting = "YYYY-MM-DD"
export default {
  /**
   * 获取本周周一和周日日期
   */
  getCurrentWeek() {
    let start = moment().weekday(1).format(formatSting); //本周一
    let end = moment().weekday(7).format(formatSting); //本周日
    return [start, end]
  },
  /**
   * 获取前 i 周的周一和周日日期，并以数组的方式返回。
   * 当 i=1，获取的是上周一和上周日的日期；
   * 当 i=2，获取的是上上周一和上上周日的日期
   * ...以此类推
   * @param i
  */
  getLastWeek(i) {
    let weekOfDay = parseInt(moment().format('E')); //计算今天是这周第几天
    let last_monday = moment().subtract(weekOfDay + 7 * i - 1, 'days').format(formatSting);//周一日期
    let last_sunday = moment().subtract(weekOfDay + 7 * (i - 1), 'days').format(formatSting);//周日日期
    return [last_monday, last_sunday]
  },
  /**
   * 获取后 i 周的周一和周日日期，并以数组的方式返回。
   * 当 i=1，获取的是下周一和下周日的日期；
   * 当 i=2，获取的是下下周一和下下周日的日期
   * ...以此类推
   * @param i
   */
  getNextWeek(i) {
    let weekOfDay = parseInt(moment().format('E'));//计算今天是这周第几天
    let next_monday = moment().add((7 - weekOfDay) + 7 * (i - 1) + 1, 'days').format(formatSting);//周一日期
    let next_sunday = moment().add((7 - weekOfDay) + 7 * i, 'days').format(formatSting);//周日日期
    return [next_monday, next_sunday]
  },
  /**
   *  最小的可使用时间 
   **/
  getMixTime() {
    return this.formatTimeStemp('2019-01-01')
  },
  /**
   * 获取前i周的所有当前周天数
   * @param i
   */
  getLastWeekDays(i) {
    const weekNumber = 7;
    const weekArray = [];
    let nowDay = moment().subtract(0, "days").format(formatSting);
    let dataTime = this.formatTimeStemp(nowDay)
    for (let index = weekNumber; index >= 1; index--) {
      let int = weekNumber * i + index
      let day = moment().subtract(int-1, 'days').format(formatSting);
      let dayTime = this.formatTimeStemp(day)
      let label = day.split('-')[2] 
      let obj = { 
        time: day,
        isAvailable: dataTime >= dayTime && this.getMixTime() <= dayTime,
        active: nowDay === day,
        dataTime: dayTime,
        label
      }
      weekArray.push(obj);
    }
    return weekArray
  },
  getLastMonthWeeks(i) {
    let monthWeeks = 3;
    let monthDay = 7
    const monthArray = [];
    let weekOfDay = parseInt(moment().format('E'));
    let firstday = moment().subtract(weekOfDay + 7 * 0 - 1, 'days').format(formatSting);
    let firstdayTime = this.formatTimeStemp(firstday)
    for (let index = monthWeeks; index >= 1; index--) {
      let int = i * monthWeeks * monthDay + (index-1) * monthDay
      let last_monday = moment().subtract(weekOfDay + int - 1, 'days').format(formatSting); //周一日期
      let last_sunday = moment().subtract(weekOfDay + int - 7 , 'days').format(formatSting); //周日日期
      let first = last_monday.replace(/-/g, '/').slice(5)
      let last = last_sunday.replace(/-/g, '/').slice(5)
      let firstTime = this.formatTimeStemp(last_monday)
      let lastTime = this.formatTimeStemp(last_sunday)
      let label = first + "-" + last
      let obj = { 
        time: [last_monday, last_sunday],
        active: last_monday === firstday,
        isAvailable: firstdayTime >= firstTime && this.getMixTime() <= firstTime,
        dataTime: firstTime,
        label 
      }
      monthArray.push(obj);
    }
    return monthArray
  },
  getLastYearMonths(i) {
    const company = 6
    const monthArray = []
    let day = moment().startOf('day').format(formatSting)
    let [ year, month ] = day.split("-")
    // let year = '2019'
    // let month = '06'
    let firstDay = year + "-" + month + "-01"
    let firstdayTime = this.formatTimeStemp(firstDay)
    let count = (i + 1) * company
    let remainder = (count - month) % 12
    let f = 12 - remainder === 12 ? 1 : 12 - remainder + 1
    let y = Math.ceil((count - month) / 12)
    for(let i = f; i< f+company; i++) {
      let s = i % 12
      let newMonth  = !s ? 12 : s
      let newYear = year - y
      if (!s) y-=1
      newMonth = newMonth < 10 ? '0' + newMonth : newMonth + ""
      let last_monday =  newYear + '-' + newMonth + '-01'
      let firstTime = this.formatTimeStemp(last_monday)
      let last_sunday = moment(last_monday).endOf('month').format(formatSting)
      let obj = { 
        time: [last_monday, last_sunday],
        active: last_monday === firstDay,
        isAvailable: firstdayTime >= firstTime && this.getMixTime() <= firstTime,
        dataTime: firstTime,
        label: newMonth + "月"
      }
      monthArray.push(obj)
    }
    return monthArray
  },
  /*
  getLastYearMonths(i) {
    const company = 6
    const monthArray = []
    let day = moment().startOf('day').format(formatSting)
    let  [ year, month ] = day.split("-")
    let m = month >= 7 ? i++ : i;
    let first =  Math.ceil((m * company - month) / 12)
    let firstDay = year + "-" + month + "-01"
    let firstdayTime = this.formatTimeStemp(firstDay)
    let num = m % 2
    for (let index = 1; index <= company; index++) {
      let base = num ? index : index + company
      base = base < 10 ? '0' + base : base
      let date = year - first + "-" + base
      let last_monday =  date + '-01'
      let firstTime = this.formatTimeStemp(last_monday)
      let last_sunday = moment(last_monday).endOf('month').format(formatSting)
      let obj = { 
        time: [last_monday, last_sunday],
        active: last_monday === firstDay,
        isAvailable: firstdayTime >= firstTime && this.getMixTime() <= firstTime,
        dataTime: firstTime,
        label: base + "月"
      }
      monthArray.push(obj)
    }
    return monthArray
  },
  */
  getNowDayTime() {
    let nowday = moment().format(formatSting)
    return this.formatTimeStemp(nowday)
  },
  // 转化为时间戳
  formatTimeStemp(value) {
    return moment(value).valueOf()
  },
  formatToChinese(value) {
    return moment(value).format('YYYY年MM月DD日')
  }
}
